using UnityEngine;
using UnityEditor;
using System.Collections.Generic;
using System.IO;
using WyrmTale;

public class InteractiveWindow : EditorWindow
{
	private TextAsset playerText;
	private TextAsset zombieText;
	private TextAsset useableText;

	private Vector2 scrollVector;
	private string addInteractiveName;
	private InteractiveType mode = InteractiveType.Player;
	private List<string> allModes = new List<string>();
	private Dictionary<InteractiveType, List<InteractiveInfo>> objects = new Dictionary<InteractiveType, List<InteractiveInfo>>();

	[MenuItem("Paperclips/Data/Interactives...")]
	private static void Init()
	{
		EditorWindow.GetWindow<InteractiveWindow>("Interactives");
	}

	private void OnFocus()
	{
		LoadAssets();

		allModes.Clear();
		allModes.AddRange(System.Enum.GetNames(typeof(InteractiveType)));
		allModes.RemoveAt(allModes.Count - 1);
	}

	private void OnGUI()
	{
		EditorGUILayout.BeginHorizontal();
		{
			GUI.color = Color.gray;
			EditorGUILayout.BeginVertical("box", GUILayout.Width(200f));
			{
				GUI.color = Color.white;
				GUILayout.Space(5f);
				GUILayout.Label("Load Type :", EditorStyles.whiteLargeLabel);
				GUILayout.Space(5f);
				foreach (string m in allModes)
				{
					if (GUILayout.Button(m + "s"))
					{
						mode = (InteractiveType)System.Enum.Parse(typeof(InteractiveType), m);
					}
				}

				GUILayout.FlexibleSpace();

				EditorGUILayout.BeginVertical("box");
				{
					GUILayout.Label("To add types to the list above");
					GUILayout.Label("you must implement a new class");
					GUILayout.Label("derived from InteractiveInfo");
					GUILayout.Label("and add that type to the ");
					GUILayout.Label("InteractiveType enum.");
				}
				EditorGUILayout.EndVertical();
				GUILayout.Space(5f);
			}
			EditorGUILayout.EndVertical();
			GUI.color = Color.white;
			scrollVector = EditorGUILayout.BeginScrollView(scrollVector);
			{
				EditorGUILayout.BeginHorizontal();
				{
					EditorGUILayout.BeginVertical();
					{
						// Draw all Items
						DrawMode(mode);
						GUILayout.FlexibleSpace();
					}
					EditorGUILayout.EndVertical();
				}
				EditorGUILayout.EndHorizontal();
			}
			EditorGUILayout.EndScrollView();
		}
		EditorGUILayout.EndHorizontal();
	}

	#region DRAW METHODS
	private void DrawMode(InteractiveType _mode)
	{
		if (!objects.ContainsKey(_mode))
		{
			objects.Add(_mode, new List<InteractiveInfo>());
		}

		InteractiveInfo.DrawInteractives(objects[_mode]);

		GUI.color = Color.gray;
		EditorGUILayout.BeginHorizontal("box");
		{
			GUI.color = Color.white;
			GUILayout.Label("Asset Name", EditorStyles.whiteLargeLabel, GUILayout.Width(100));
			addInteractiveName = EditorGUILayout.TextField(addInteractiveName, GUILayout.Width(150));

			if (GUILayout.Button("Add " + _mode.ToString(), GUILayout.Width(100)) && addInteractiveName != "")
			{
				InteractiveInfo info = null;
				if (objects[_mode].Count > 0)
				{
					// make a copy of the last data
					info = objects[_mode][objects[_mode].Count - 1].Clone();
				}
				else
				{
					if (_mode == InteractiveType.Player)
					{
						info = ScriptableObject.CreateInstance<CharacterInfo>();
					}
					else if (_mode == InteractiveType.Zombie)
					{
						info = ScriptableObject.CreateInstance<ZombieInfo>();
					}
					else if (_mode == InteractiveType.Key)
					{
						info = ScriptableObject.CreateInstance<KeyInfo>();
					}
					else if (_mode == InteractiveType.Container)
					{
						info = ScriptableObject.CreateInstance<ContainerInfo>();
					}
					else
					{
						info = ScriptableObject.CreateInstance<UseableInfo>();
					}
				}

				info.itemName = addInteractiveName;
				InteractiveManager.SaveAsset(info, _mode.ToString(), addInteractiveName);

				LoadAssets();
				addInteractiveName = "";
			}
		}
		EditorGUILayout.EndHorizontal();
	}
	#endregion

	#region SAVE METHODS
	/*private void SaveAssets()
	{
		for (int i = 0; i < (int)InteractiveType.COUNT; i++)
		{
			InteractiveType type = (InteractiveType)i;
			if (objects.ContainsKey(type))
			{
				foreach (InteractiveInfo info in objects[type])
				{
					InteractiveManager.SaveAsset(info, type.ToString());
				}
			}
		}

		assetAdded = false;
	}*/

	private void LoadAssets()
	{
		InteractiveManager.LoadAssetDictionary(ref objects);
	}

	/*private void UpdateAssets()
	{
		Dictionary<InteractiveType, List<InteractiveInfo>> loaded = new Dictionary<InteractiveType, List<InteractiveInfo>>();
		InteractiveManager.LoadAssetDictionary(ref loaded);

		List<InteractiveInfo> currentAssets = new List<InteractiveInfo>();
		List<InteractiveInfo> loadedAssets = new List<InteractiveInfo>();
		
		foreach (KeyValuePair<InteractiveType, List<InteractiveInfo>> kvp in objects)
		{
			currentAssets.AddRange(kvp.Value);
		}
		foreach (KeyValuePair<InteractiveType, List<InteractiveInfo>> kvp in loaded)
		{
			loadedAssets.AddRange(kvp.Value);
		}

		foreach (InteractiveInfo info in loadedAssets)
		{
			if (!currentAssets.Contains(info))
			{
				InteractiveManager.DestroyAsset(info);
			}
		}

		assetRemoved = false;
	}*/
	#endregion
}